﻿using System;
using GameFramework.configs;
using GameFramework.net.msgserializer;
using UnityEngine;
using Object = System.Object;

namespace GameFramework.net
{
    // 消息魔术(2字节)+消息长度(4字节)+消息ID(4字节)+消息版本(4字节)+序列化类型(1字节)+消息内容
    public class MsgWrapper
    {
        public short moshu;//魔术字符串(2字节) 0xfafa
        public int msgSize;//消息长度[消息内容的长度]
        public int msgId;//消息id
        public int version;//消息版本
        public int serializeType;//序列化类型(前后端约定好，1json,2protobuf,3自定义)
        public byte[] rawMsg;//接收的原始数据(字节数组)或待发送的原始数据(对象类型)
        public object deSerialize;//接收数据反序列化后的数据
        
        public object sendRawMsg;//待发送的原始数据
        public byte[] sendSerializeMsg;//待发送的序列化后的数据

        public static int MsgHeadLenth = 2+4+4+4+1;//消息头长度


        public static MsgWrapper createSendMsgWrapper(int msgId,int msgSize,object msg,byte[] serializedMsg)
        {
            MsgWrapper msgWrapper = new MsgWrapper();
            //这下面这3个可以从配置文件中读取，不需要配置构造传入
            msgWrapper.moshu = LocalConfig.Instance.msgConfig.moshu;
            msgWrapper.version = LocalConfig.Instance.msgConfig.version;
            msgWrapper.serializeType = LocalConfig.Instance.msgConfig.serializeType;
            
            msgWrapper.msgId = msgId;
            msgWrapper.msgSize = msgSize;
            msgWrapper.sendRawMsg = msg;
            msgWrapper.sendSerializeMsg = serializedMsg;
            return msgWrapper;
        }

        public static MsgWrapper createReceiveMsgWrapper(int msgId,int msgSize,byte[] rawMsg,object deSerializedMsg)
        {
            MsgWrapper msgWrapper = new MsgWrapper();
            //这下面这3个可以从配置文件中读取，不需要配置构造传入
            msgWrapper.moshu = LocalConfig.Instance.msgConfig.moshu;
            msgWrapper.version = LocalConfig.Instance.msgConfig.version;
            msgWrapper.serializeType = LocalConfig.Instance.msgConfig.serializeType;
            
            msgWrapper.msgId = msgId;
            msgWrapper.msgSize = msgSize;
            msgWrapper.rawMsg = rawMsg;
            msgWrapper.deSerialize = deSerializedMsg;
            return msgWrapper;
        }
    }
}